
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/wait.h>
#include <sys/types.h>

#include <string>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

int GetFields(char *pBuf, char *pLst[20], int mNum);

int main(int argc, char* argv[])
{
	if(argc < 2)
	{
		printf("%s (dat file)\n", argv[0] );
		return 0;
	}	
	int mCount = 0, num = 0, spamnum = 0, n;	
	char buf[4096] = {0};
	char tmp[4096] = {0};
	char tmp2[256] = {0};
	char *pLst[20];
	
	map<string, int> mChannMap;
	map<string, int> mChannHHMap;
	map<string, int>::iterator it, hit;
	map<string, int> mSpamChannMap;
	//vector<string> mChannVec;
	
	FILE *fpdat = fopen(argv[1], "r");
	if(fpdat == NULL) return 0;
		
	while( fgets(buf, 4096, fpdat) )
	{		
		mCount++;
		if( GetFields(buf, pLst, 18) < 0) continue;
		num++;
		if( strlen(pLst[0]) == 19 )
		{
			it = mChannMap.find(pLst[7]);
			if( it == mChannMap.end() )
				mChannMap.insert(map<string, int>::value_type(pLst[7], 1));
			else
				it->second++;
			
			memcpy(tmp2, pLst[0]+11, 2);
			tmp2[2] = 0;
			sprintf(tmp, "%s_%s", pLst[7], tmp2);
			it = mChannHHMap.find(tmp);
			if( it == mChannHHMap.end() )
				mChannHHMap.insert(map<string, int>::value_type(tmp, 1));
			else
				it->second++;
		}
		memset(buf, 0, 1024);
	}
	fclose(fpdat);
	printf("%s\tcount = %d\tnum = %d %.2f%%\n\n", argv[1], mCount, num, num*100.0/mCount );
	
	hit=mChannHHMap.begin();
	strcpy(tmp, hit->first.c_str());
	char *p = strchr(buf, '_');
	if(p) *p = 0;
	n = 0;
	for(hit=mChannHHMap.begin(); hit!=mChannHHMap.end(); hit++ )
	{
		strcpy(buf, hit->first.c_str());
		p = strchr(buf, '_');
		if(p)
		{
			*p = 0;
			if( strncmp(tmp, buf, strlen(buf) ) == 0 )
			{
				n++;
			}
			else
			{
				it = mChannMap.find(tmp);
				if(it != mChannMap.end() && ((it->second > 100 && n < 10) || (it->second > 500 && n >= 10 && n < 20)) )
				{
					spamnum += it->second;
					printf("%s\t%d\t%d\n", tmp, n, it->second);
					//mChannVec.push_back( tmp );
					mSpamChannMap.insert(map<string, int>::value_type(tmp, 1));
				}
				n = 0;
				strcpy(tmp, buf);
			}
		}
		else
			buf[0] = 0;		
	}
	it = mChannMap.find(tmp);
	if(it != mChannMap.end() && ((it->second > 100 && n < 10) || (it->second > 500 && n >= 10 && n < 20)) )
	{
		spamnum += it->second;
		printf("%s\t%d\t%d\n", tmp, n, it->second);
	}
	if( spamnum <= 0 ) return 0;
		
	printf("\n%s\tcount = %d\tspam num = %d %.2f%%\tchann num = %zu\n\n", argv[1], mCount, spamnum, spamnum*100.0/mCount, mSpamChannMap.size() );
	
	sprintf(tmp, "%s.aid", argv[1]);
	FILE *fpdatR = fopen(tmp, "w");
	if(fpdatR == NULL) return 0;
		
	fpdat = fopen(argv[1], "r");
	if(fpdat == NULL) return 0;
		
	while( fgets(buf, 4096, fpdat) )
	{		
		mCount++;
		strcpy(tmp, buf);
		
		if( GetFields(buf, pLst, 18) < 0) continue;
		num++;
		if( strlen(pLst[0]) == 19 )
		{
			it = mSpamChannMap.find(pLst[7]);
			if( it != mSpamChannMap.end() )
				fprintf(fpdatR, "%s", tmp);
		}
		memset(buf, 0, 1024);
	}
	fclose(fpdat);
	fclose(fpdatR);
	
	mChannMap.clear();
	mChannHHMap.clear();
	mSpamChannMap.clear();
	return 0;
}

int GetFields(char *pBuf, char *pLst[20], int mNum = 18)
{
	char *p = NULL;
	int i = 0;
	memset(pLst, 0, sizeof(char *)*20);
	if(mNum>18) mNum = 18;
	pLst[0] = pBuf;
	for( i = 1; i<mNum; i++)
	{
		p = strchr(pLst[i-1], '\t');
		if(!p) return -1;
		if(*p == '\t' || *p == '\r' || *p == '\n')
		{
			*p++ = 0;
			if( (p-pLst[i-1]) > 2048 ) pLst[i-1][2048] = 0;
			pLst[i] = p;
		}
	}
	if( pLst[mNum-1] )
	{
		p = pLst[mNum-1];
		while(p && *p && *p != '\r' && *p != '\n') p++;
		*p = 0;
	}
	return 0;
}
